﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CrclzChat.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace CrclzChat.Api.Register
{
	[Route("api/Register/[controller]")]
	[ApiController]
	public class CheckUsernameController : ControllerBase
	{
		private OneContext _context { get; set; }

		public CheckUsernameController(OneContext context)
		{
			_context = context;
		}

		[HttpPost]
		public async Task<IActionResult> Post([FromForm]string Username)
		{
			var normalizedUsername = Username.ToUpper();

			var isExist = await _context.Users
				.AnyAsync(p => p.NormalizedUserName == normalizedUsername);

			var isValid = !isExist;

			return new JsonResult(isValid);
		}
	}
}